home *** CD-ROM | disk | FTP | other *** search
/ Aminet 5 / Aminet 5 - March 1995.iso / Aminet / util / misc / zero.lha / zero.c < prev    next >
C/C++ Source or Header  |  1995-01-09  |  4KB  |  151 lines

  1. /* zero-handler - source of 0-bytes, like /dev/zero in Unix
  2.  *
  3.  * Copyright (C) 1994 by Ingo Wilken (Ingo.Wilken@informatik.uni-oldenburg.de)
  4.  *
  5.  * Permission to use, copy, modify, and distribute this software and its
  6.  * documentation for any purpose and without fee is hereby granted, provided
  7.  * that the above copyright notice appear in all copies and that both that
  8.  * copyright notice and this permission notice appear in supporting
  9.  * documentation.  This software is provided "as is" without express or
  10.  * implied warranty.
  11.  *
  12.  * V1.0: 23/Oct/94 first version
  13.  * V1.1: 06/Jan/95 added bytecount
  14.  * V1.2: 09/Jan/95 minor cleanup
  15.  */
  16. #define THIS_PROGRAM    "zero-handler"
  17. #define THIS_VERSION    "1.2"
  18.  
  19. static const char amiga_version[] = "\0$VER: " THIS_PROGRAM " " THIS_VERSION " (" __COMMODORE_DATE__ ")";
  20.  
  21.  
  22. #include <exec/types.h>
  23. #include <exec/nodes.h>
  24. #include <exec/lists.h>
  25. #include <exec/ports.h>
  26. #include <exec/memory.h>
  27. #include <dos/dos.h>
  28. #include <dos/dosextens.h>
  29. #include <dos/filehandler.h>
  30. #include <proto/exec.h>
  31. #include <proto/dos.h>
  32. #include <stdlib.h>
  33. #include <string.h>
  34. #include <clib/alib_protos.h>
  35.  
  36. static char * BSTR2C(BPTR bstr);
  37.  
  38.  
  39. static struct MinList mylist;
  40. typedef struct {
  41.     struct MinNode  node;
  42.     LONG            bytesleft;
  43. } NODE;
  44.  
  45.  
  46. static char *
  47. BSTR2C(bstr)
  48.     BPTR bstr;
  49. {
  50.     static char cstr[256];
  51.     int len;
  52.     UBYTE *p = (UBYTE *)BADDR(bstr);
  53.  
  54.     if( p ) {
  55.         len = (int) *p++;
  56.         strncpy(cstr, (char *)p, len);
  57.         cstr[len] = '\0';
  58.         return cstr;
  59.     }
  60.     return (char *)0;
  61. }
  62.  
  63.  
  64. void _main()
  65. {
  66.     struct DeviceNode *devnode;
  67.     struct Process *myproc;
  68.     struct DosPacket *pkt;
  69.     NODE *node;
  70.     struct FileHandle *fh;
  71.     char *name;
  72.     LONG err1, err2;
  73.  
  74.     NewList((struct List *)&mylist);
  75.  
  76.     myproc = (struct Process *)FindTask(NULL);
  77.  
  78.     /* get the startup message */
  79.     pkt = WaitPkt();
  80.     devnode = (struct DeviceNode *)BADDR(pkt->dp_Arg3);
  81.     devnode->dn_Task = &(myproc->pr_MsgPort);
  82.     ReplyPkt(pkt, DOSTRUE, 0);
  83.  
  84.     for(;;) {
  85.         pkt = WaitPkt();
  86.  
  87.         err1 = DOSTRUE; err2 = 0;
  88.         switch( pkt->dp_Type ) {
  89.             case ACTION_FINDINPUT:
  90.                 fh = (struct FileHandle *)BADDR(pkt->dp_Arg1);
  91.                 fh->fh_Port = NULL;
  92.  
  93.                 node = (NODE *)0;
  94.                 if( name = BSTR2C(pkt->dp_Arg3) ) {
  95.                     char *p; LONG nbytes;
  96.                     if( p = strchr(name, ':') )
  97.                         name = ++p;
  98.                     nbytes = strtol(name, &p, 10);
  99.                     if( *p ) {             /* "zero:xxx" */
  100.                         err1 = DOSFALSE;
  101.                         err2 = ERROR_OBJECT_NOT_FOUND;
  102.                     }
  103.                     else
  104.                     if( name == p )             /* "zero:\0" */
  105.                         fh->fh_Arg1 = (LONG)0;
  106.                     else                        /* "zero:<number>" */
  107.                     if( node = AllocMem(sizeof(NODE), MEMF_ANY|MEMF_CLEAR) ) {
  108.                         node->bytesleft = nbytes;
  109.                         AddTail((struct List *)&mylist, (struct Node *)node);
  110.                     }
  111.                     else {
  112.                         err1 = DOSFALSE;
  113.                         err2 = ERROR_NO_FREE_STORE;
  114.                     }
  115.                 }
  116.                 fh->fh_Arg1 = (LONG)node;
  117.             break;
  118.  
  119.             case ACTION_END:
  120.                 if( node = (NODE *)(pkt->dp_Arg1) ) {
  121.                     Remove((struct Node *)node);
  122.                     FreeMem(node, sizeof(NODE));
  123.                 }
  124.             break;
  125.  
  126.             case ACTION_READ:
  127.                 err1 = pkt->dp_Arg3;
  128.  
  129.                 if( node = (NODE *)(pkt->dp_Arg1) ) {
  130.                     if( err1 > node->bytesleft )
  131.                         err1 = node->bytesleft;
  132.                     node->bytesleft -= err1;
  133.                 }
  134.                 bzero((void *)(pkt->dp_Arg2), err1);
  135.             break;
  136.  
  137.             case ACTION_IS_FILESYSTEM:
  138.                 err1 = DOSFALSE;
  139.                 /* err2 = 0; */
  140.             break;
  141.  
  142.             default:
  143.                 err1 = DOSFALSE;
  144.                 err2 = ERROR_ACTION_NOT_KNOWN;
  145.             break;
  146.         }
  147.         ReplyPkt(pkt, err1, err2);
  148.     }
  149. }
  150.  
  151.